<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            padding-bottom: 800px;
        }
        .box{
            border: 1px solid black;
            padding: 20px;
        }
    </style>
</head>
<body>
    <div id="app" class="box">
        <h4>父组件</h4>
        <comp-one :msg="tempMsg"></comp-one>
        <br>
        <comp-two @update:msg="updateMsg"></comp-two>
        <br>
        <comp-three :msg="tempMsg" :info="tempInfo"></comp-three>
        <br>
        <comp-four @update:info="updateInfo"></comp-four>
    </div>

    <script type="text/x-template" id="compOne">
        <div class="box">
            <h4>子组件A</h4>
            <p>msg:{{ msg }}</p>
        </div>
    </script>
    <script type="text/x-template" id="compTwo">
        <div class="box">
            <h4>子组件B</h4>
            <p>msg:{{ msg }}</p>
            <input type="button" value="发送msg" @click=" sendMsg() ">
        </div>
    </script>
    <script type="text/x-template" id="compThree">
        <div class="box">
            <h4>子组件C</h4>
            <comp-three-child v-bind="$attrs"></comp-three-child>
        </div>
    </script>
    <script type="text/x-template" id="compFour">
        <div class="box">
            <h4>子组件D</h4>
            <comp-four-child v-bind="$attrs"></comp-four-child>
        </div>
    </script>
    <script type="text/x-template" id="compThreeChild">
        <div class="box">
            <h4>子组件C-A</h4>
            <p>info:{{ info }}</p>
        </div>
    </script>
    <script type="text/x-template" id="compFourChild">
        <div class="box">
            <h4>子组件D-A</h4>
            <p>info:{{ info }}</p>
            <input type="text" v-model="info">
        </div>
    </script>

    <script type="module"> 
        import { createApp } from "../../assets/vue3/vue.esm-browser.js";
        const app = createApp({
            data(){
                return {
                    tempMsg:"",
                    tempInfo:""
                }
            },
            methods: {
                updateMsg(nv) {
                    this.tempMsg = nv;
                },
                updateInfo(nv){
                    this.tempInfo = nv;
                }
            },
        })
        
        app.component("CompOne",{
            template:"#compOne",
            props:["msg"]
        })
        app.component("CompTwo",{
            template:"#compTwo",
            emits:["update:msg"],
            data() {
                return {
                    msg: "组件B的数据msg"
                }
            },
            methods: {
                sendMsg() {
                    this.$emit("update:msg",this.msg)
                }
            },
        })
        app.component("CompThree",{
            template:"#compThree",
            props:["msg"]
        })
        app.component("CompFour",{
            template:"#compFour",
        })
        app.component("CompThreeChild",{
            template:"#compThreeChild",
            props:["info"]
        })
        app.component("CompFourChild",{
            template:"#compFourChild",
            emits:["update:info"],
            data() {
                return {
                    info: "组件D的子组件数据info"
                }
            },
            watch:{
                info(){
                    this.$emit("update:info",this.info)
                }
            }
        })
        
        app.mount("#app")
    </script>
</body>
</html>